package com.wb.mianxiangduixiang.bms;

import java.util.Scanner;

/**
 * @author 黄衡熙
 * @version 1.0
 * @date 2025年07月22日15:14
 */
public class Start {

    private Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Start().showMenu();
    }

    public void showMenu() {
        System.out.println("******************欢迎来到紫金银行******************");
        System.out.print("请选择服务(1.开户 2.登录):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            default:
                System.out.println("提示:您输入的选项有误,已退出");
                break;
        }
    }

    public void register() {
        System.out.print("请选择开户的账户类型(1.储蓄账户 2.信用账户 3.贷款的储蓄账户 4.贷款的信用账户):");
        Account account=null;
        System.out.print("请输入开户类型:");
        int choice=sc.nextInt();
        switch (choice) {
            case 1:
                account=new SavingAccount();
                break;
            case 2:
                account=new CreditAccount();
                break;
            case 3:
                account=new LoanSavingAccount();
                break;
            case 4:
                account=new LoanCreditAccount();
                break;
            default:
                System.out.print("您的输入有误,请重新输入:");
                register();

        }
        // 填写开户所需要的信息
        System.out.print("请输入户主姓名:");
        account.setName(sc.next());
        int count=0;
        while(true){
            count++;
            System.out.print("请输入账户密码:");
            String password=sc.next();
            System.out.print("请再次输入密码:");
            String repassword=sc.next();
            if(password.equals(repassword)){
                account.setPassword(password);
                break;
            }else{
                System.out.print("两次输入密码不一致，请重新输入:");
            }
            if(count==3){
                System.out.println("密码错误已达上限");
                return;
            }
        }
        System.out.print("请输入身份证号:");
        account.setPersonId(sc.next());
        System.out.print("请输入预留手机号:");
        account.setPhone(sc.next());
        account.setBalance(0.0);
        if(account instanceof CreditAccount){
            System.out.print("请输入您的透支额度:");
            CreditAccount creditAccount=(CreditAccount) account;
                creditAccount.setLimit(sc.nextDouble());
        }
        // 当所有信息全部填写完成之后,调用银行提供的开户功能进行开户
        Bank.register(account);
        System.out.println("恭喜您开户成功，您的卡号为:"+account.getId());


        // 当开户成功之后回到入口程序主菜单,继续选择其他服务
        this.showMenu();
    }

    public void login() {
        System.out.print("请输入卡号:");
        int id = sc.nextInt();
        System.out.print("请输入密码:");
        String password = sc.next();

        // 根据银行Bank提供的登录方法判断当前输入的卡号与密码是否正确
        Account account = Bank.login(id, password);
        if (account == null) {
            System.out.println("提示账户或者密码错误");
            showMenu();
            // 如果返回值为null,说明卡号或者密码有误
        } else {
            System.out.println("登陆成功");
            new BankMenu(account).showMenu();
            // 否则,登录成功,进入到银行主菜单
        }

    }

}